/*
 * @(#) ListOfficeTag .java  1.0  2004-4-12
 *
 * Copyright 2003 by LongTop Corporation.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 */
package com.cnc.tag;

import com.cnc.bas.util.StringUtil;
import com.cnc.ssm.ISsmService;
import com.cnc.ssm.UserInfo;

import org.apache.struts.taglib.html.Constants;
import org.apache.struts.taglib.html.SelectTag;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * User: Du bf
 * Date: 2004-10-18
 */
public class ListInstTag extends LtcmTag {
    //�Ƿ�����༭
    protected boolean disabled = false;
    //�Ƿ���������һ���Ԫ��
    private boolean condition = false;
    //�������
    private String areaCode;
    //�����
    private String instCode;
    //����
    private String instLevel;
    //�Ƿ��г�����֧��
    private boolean listAll = false;
    //�Ƿ���ʾ�����
    private boolean showCode = false;
    //�Ƿ���ʾ����
    private boolean showInstLevel = false;

    public boolean isDisabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public boolean isCondition() {
        return condition;
    }

    public void setCondition(boolean condition) {
        this.condition = condition;
    }

    public String getAreaCode() {
        return areaCode;
    }

    public void setAreaCode(String areaCode) {
        this.areaCode = areaCode;
    }

    public String getInstCode() {
        return instCode;
    }

    public void setInstCode(String instCode) {
        this.instCode = instCode;
    }

    public String getInstLevel() {
        return instLevel;
    }

    public void setInstLevel(String instLevel) {
        this.instLevel = instLevel;
    }

    public boolean isListAll() {
        return listAll;
    }

    public void setListAll(boolean listAll) {
        this.listAll = listAll;
    }

    public boolean isShowCode() {
        return showCode;
    }

    public void setShowCode(boolean showCode) {
        this.showCode = showCode;
    }

    public boolean isShowInstLevel() {
        return showInstLevel;
    }

    public void setShowInstLevel(boolean showInstLevel) {
        this.showInstLevel = showInstLevel;
    }

    public int doEndTag() throws JspException {
        HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
        ISsmService ssmService = (ISsmService) this.getBean(com.cnc.bas.util.Constants.BEAN_NAME_SSM);
        UserInfo oper = (UserInfo) request.getSession().getAttribute(com.cnc.bas.util.Constants.USER_KEY);
        List list = null;
        String strInstCode = this.instCode;
//      liuwei修改过
//        String strInstLevel = oper.getInstLevel();
//
//        if (strInstCode == null && strInstLevel.equals("3"))
//            strInstCode = oper.getInstCode();
        strInstCode = strInstCode == null ? "" : strInstCode;
        if (listAll) {
            strInstCode = "";
        }
        list = ssmService.listInst(strInstCode, (instLevel == null ? "" : instLevel));
        Iterator iter = list.iterator();
        StringBuffer results = new StringBuffer();
        if (condition ){//|| strInstLevel.equals("1")) {
            results.append("<option value=''");
            if (disabled) {
                results.append(" disabled=\"disabled\"");
            }
            results.append(">");

            results.append("</option>");
        }
        while (iter.hasNext()) {
            HashMap inst = (HashMap) iter.next();
            String instNo = String.valueOf(inst.get("INST_CODE")).trim();
            String instName = String.valueOf(inst.get("INST_NAME")).trim();
            results.append("<option value=\"" + instNo + "\"");
            if (disabled) {
                results.append(" disabled=\"disabled\"");
            }
            if (showInstLevel) {
                results.append(" instLevel=\"" + String.valueOf(inst.get("INST_LEVEL")).trim() + "\"");
            }
            SelectTag selecttag = this.selectTag();
            if (selecttag != null) {
                if (selecttag.isMatched(instNo)) {
                    results.append(" selected=\"selected\"");
                }
            }
            results.append(">");
            if (this.showCode)
                results.append(StringUtil.toFixLength(instNo, 20) + "  |  ");
            results.append(instName);
            results.append("</option>");
        }
        ResponseUtils.write(pageContext, results.toString());
        return (EVAL_PAGE);
    }

    private SelectTag selectTag() throws JspException {
        SelectTag selectTag =
                (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY);

        if (selectTag == null) {
            JspException e =
                    new JspException("error : no select element");

            RequestUtils.saveException(pageContext, e);
            throw e;
        }
        return selectTag;
    }

}
